package com.sdry.web.controller.base;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.sdry.entity.Content;
import com.sdry.entity.base.Staff;
import com.sdry.service.ContentService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONObject;

@RequestMapping(value="/contentMana")
@Controller
public class ContentController {

	@Resource
	private ContentService contentService;
	
	@RequestMapping(value="/listPageContent")
	public void listPageContent(@RequestParam(value = "limit",required = false)int limit,
            @RequestParam(value = "offset",required = false)int offset,
            Content content,HttpServletResponse response,HttpSession session) throws Exception{
		Staff userSession = (Staff) session.getAttribute("staff");
		Map<String, Object> map = new HashMap<>();
		map.put("staffID", userSession.getStaid());
		map.put("pageSize", limit);
		map.put("page", offset);
		if (content.getTitle() != null) {
			map.put("title", content.getTitle());
		} else {
			map.put("title", "");
		}
		List<Content> contentList = contentService.listPageContent(map);
		PageInfo<Content> info = new PageInfo<Content>(contentList);
    	
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", info.getTotal());
        jsonObject.put("rows", info.getList());
        ResponseUtil.write(response, jsonObject);
	}
	
	@RequestMapping(value="/saveContent")
	public void saveContent(Content content,HttpServletResponse response,HttpSession session) throws IOException{
		int isSuccess = 0;
		Staff userSession = (Staff) session.getAttribute("staff");
		content.setStaffID(userSession.getStaid());
		
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String format = dateFormat.format(date);
		content.setReserved5(format);
		
		if (content.getContentID() == null || "".equals(content.getContentID())) {
			isSuccess = contentService.insertContent(content);
		} else {
			isSuccess = contentService.updateContent(content);
		}
		if (isSuccess == 1) {
			response.getWriter().print("1");
		} else {
			response.getWriter().print("0");
		}
	}
	
	@RequestMapping("/deleteContent")
	public void deleteContent(String ids,HttpServletRequest request,HttpSession session, HttpServletResponse response) throws Exception{
		int isSuceess = 0;
		String[] contentIDs = ids.split(",");
		isSuceess = contentService.deleteContent(contentIDs);
		if (isSuceess >= 1) {
			response.getWriter().print("1");
		}else{
			response.getWriter().print("0");
		}
	}
	
	@RequestMapping(value="/pageToContentForm")
	public ModelAndView pageToContentForm(Long contentID){
		ModelAndView mv = new ModelAndView();
		Content content = contentService.findContentByID(contentID);
		mv.addObject("content", content);
		mv.setViewName("/base/content/contentForm");
		return mv;
	}
}
